package net.molchat.oauthorize.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import net.molchat.spring.util.SpringUtil;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {


/**
 * Simply selects the home view to render by returning its name.
 * 
 * @param request
 * @param model
 * @return View name
 */
@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {

	String userLogin;
	boolean isAdmin = false;
	boolean isUser = false;

	UserDetails principal = SpringUtil.getUser();
	if (principal != null) {
		userLogin = principal.getUsername();
		isAdmin = request.isUserInRole("ROLE_ADMIN");
		isUser = request.isUserInRole("ROLE_USER");
	} else {
		userLogin = "not logged in"; // Any default user  name
	}

	model.addAttribute("serverTime", new Date().toString());
	model.addAttribute("userLogin", userLogin);
	model.addAttribute("isUser", isUser);
	model.addAttribute("isAdmin", isAdmin);

	return "home";
}


}
